<?php
include('../../includes/classes/core.php');


$action	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error	= '';
$data	= '';

switch ($action) {
	case 'get_edit_page':
		$produsID	= $_REQUEST['id'];
		$page 		= GetPage( GetCartridge($produsID) );
		$data		= array('page'	=> $page);

		break;
	case 'get_prod_list':
		$produsID	= $_REQUEST['id'];
		$data = array(
				"aaData"	=> array()
		);

		break;
	case 'unknown_move':
	    
	    mysql_query("UPDATE `warehouse` SET `actived`='$_REQUEST[status]' WHERE (`id`='$_REQUEST[id]')");
	    
	    break;	
		
		
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];

	    $rResult = mysql_query("SELECT		`warehouse`.`id`,
	    						`warehouse`.`start_date`,
								`warehouse`.`end_date`,
								CONCAT( '№: დ-',`warehouse`.`d_letter_id`)	 as `letter`,
								CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `client`,
	                            CASE (d_letter.`priority`)
                                            		WHEN 1  THEN 'ჩვეულებრივი'
                                            		WHEN 2  THEN 'საშუალო'
                                            	    WHEN 3  THEN 'მაღალი'
                                END as `priority`,
								CASE             WHEN `warehouse`.`producing_status` = 0 THEN 'მიღებული&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&nbsp;<img height=17 width=17 src=\"media/images/unread.png\">'
									             WHEN `warehouse`.`producing_status` = 1 THEN 'მიღებული&emsp;&emsp;&nbsp;&emsp;&emsp;&emsp;&emsp;<img height=17 width=17 src=\"media/images/read.png\">'
									             WHEN `warehouse`.`producing_status` = 2 THEN 'წარმ. დასრულებულია&nbsp;<img height=17 width=17 src=\"media/images/greenflag.png\">'
									        END AS ds
								FROM		`warehouse`
								LEFT JOIN	`client_objects` ON `client_objects`.`id` = `warehouse`.`client_object_id`
	                            LEFT JOIN 	d_letter  ON `d_letter`.`id` = `warehouse`.`d_letter_id`
								LEFT JOIN	client ON client_objects.client_id = client.id
								WHERE		!ISNULL(warehouse.producing_status) AND !ISNULL(`warehouse`.`d_letter_id`) AND warehouse.status < 5 AND `warehouse`.`actived` = $_REQUEST[status]
	        
	        ORDER BY d_letter.`priority` DESC, warehouse.end_date ASC
	        ");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count; $i++ )
			{
				// General output
				$row[] = $aRow[$i];
			}

			$data['aaData'][] = $row;
		}

        break;
	case 'recive':
		$produsID	= $_REQUEST['id'];
		$type		= $_REQUEST['type'];

		if( $type == '1'){
			Arecive($produsID);
		}else if( $type == '2' ){
			Drecive($produsID);
		}else{
			recive($produsID);
		}

		break;

	case 'end_producing':
		$produsID	= $_REQUEST['id'];
		$type		= $_REQUEST['type'];
		if( CheckCArtridgeStatus($produsID) ){
			$error = 'ყველა პრინტერზე მუშაობა არ არის დასრულებილი!';
			break;
		}

		if( $type == '1'){
			AendProducing($produsID);
		}else if( $type == '2' ){
			DendProducing($produsID);
		}else{
			endProducing($produsID);
		}

		break;
	case 'check':
		$data		= array('message'	=> check());

		break;
	case 'get_warehouse_id' :
		$barcode	= $_REQUEST['barcode'];
		$data		= array('warehouseID'	=> GetWarehouseId($barcode));
		break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);

function ChangeLocation($produsID, $type){

	$csql = mysql_query("	SELECT	`warehouse_detail`.`barcode` as `barcode`
							FROM		`warehouse`
							LEFT JOIN	`warehouse_detail` ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id`
							WHERE   `warehouse`.`id` = '$produsID'");
	$location = '';
	if( $type == '0' ){
		$location = 130;
	}else if( $type == '1' ){
		$location = 134;
	}else{
		$location = 133;
	}

	while ( $aRow = mysql_fetch_array( $csql ) ){
			mysql_query("	UPDATE 	`reserve`
							SET 	`reserve`.`object_id` = '$location'
							WHERE	`reserve`.`barcode` = '$aRow[barcode]' ");
	}
}

function recive($produsID){
	$csql = mysql_query("SELECT		`producing_status`
			FROM		`warehouse`
			WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '0'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 1,
		        `actived`= 1  		
		WHERE	id = '$produsID'");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 3,
		        `actived`= 1  
		        
		WHERE	id = '$produsID'");

		ChangeLocation($produsID, 0);
	}
}

function Arecive($produsID){
	$csql = mysql_query("SELECT		`producing_status`
						FROM		`warehouse`
						WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '0'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 1,
		        `actived`= 1
		WHERE	id = '$produsID'");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 3,
		        `actived`= 1
		WHERE	id = '$produsID'");

		$csql = mysql_query("SELECT		`warehouse`.`a_letter_id`
							 FROM		`warehouse`
							 WHERE		`warehouse`.`id` = '$produsID'");
		$csql 		= mysql_fetch_array( $csql );
		if($csql[0] != ''){
			mysql_query("
			UPDATE	`a_letter`
			SET		`status` = 2
			WHERE	`id` 	= '$csql[0]'");

			$c_data = date('Y-m-d H:i:s');

			mysql_query("
			UPDATE	`a_letter`
			SET		`job_start_date` = '$c_data'
			WHERE	`id` 	= '$csql[0]'");

			ChangeLocation($produsID, 0);
		}
	}
}

function Drecive($produsID){
	$csql = mysql_query("SELECT		`producing_status`
						FROM		`warehouse`
						WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '0'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 1,
		        `actived`= 1
		WHERE	id = '$produsID'");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 3,
		        `actived`= 1
		WHERE	id = '$produsID'");

		ChangeLocation($produsID, 0);

		$csql = mysql_query("SELECT		`warehouse`.`d_letter_id`
							FROM		`warehouse`
							WHERE		`warehouse`.`id` = '$produsID'");
		$csql 		= mysql_fetch_array( $csql );
		if($csql[0] != ''){
			mysql_query("
			UPDATE	`d_letter`
			SET		`status` = 2
			WHERE	`id` 	= '$csql[0]'");

			$c_data = date('Y-m-d H:i:s');

			mysql_query("
			UPDATE	`d_letter`
			SET		`job_start_date` = '$c_data'
			WHERE	`id` 	= '$csql[0]'");


		}
	}
}

function endProducing($produsID){
	$csql = mysql_query("	SELECT		`producing_status`
							FROM		`warehouse`
							WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '1'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 2,
		        `actived`= 1
		WHERE	id = '$produsID'");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 6,
		        `actived`= 1
		WHERE	id = '$produsID'");

		ChangeLocation($produsID, 2);
	}
}

function AendProducing($produsID){
	recive($produsID);
	$csql =mysql_query("SELECT		`producing_status`
						FROM		`warehouse`
						WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '1'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 2,
		        `actived`= 1
		WHERE	id = $produsID");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 4,
		        `actived`= 1
		WHERE	id = $produsID");

		ChangeLocation($produsID, 1);


		$aletter = mysql_query("SELECT		`warehouse`.`a_letter_id`
								FROM		`warehouse`
								WHERE		`warehouse`.`id` = '$produsID'");
		$aletter 		= mysql_fetch_array( $aletter );
		if($aletter[0] != ''){
			mysql_query("UPDATE	`a_letter`
						SET		`status` = 3
						WHERE	`id` 	= '$aletter[0]'");

			$c_data = date('Y-m-d H:i:s');

			mysql_query("UPDATE	`a_letter`
						SET		`job_end_date` = '$c_data'
						WHERE	`id` 	= '$aletter[0]'");
		}
	}
}

function DendProducing($produsID){
	recive($produsID);
	$csql =mysql_query("SELECT		`producing_status`
			FROM		`warehouse`
			WHERE		`id` = '$produsID'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '1'){
		mysql_query("
		UPDATE	`warehouse`
		SET		`producing_status` = 2,
		        `actived`= 1
		WHERE	id = $produsID");

		mysql_query("
		UPDATE	`warehouse`
		SET		`status` = 4,
		        `actived`= 1
		WHERE	id = $produsID");

		ChangeLocation($produsID, 1);


		$aletter = mysql_query("SELECT		`warehouse`.`d_letter_id`
				FROM		`warehouse`
				WHERE		`warehouse`.`id` = '$produsID'");
		$aletter 		= mysql_fetch_array( $aletter );
		if($aletter[0] != ''){
			mysql_query("UPDATE	`d_letter`
			SET		`status` = 3
			WHERE	`id` 	= '$aletter[0]'");

			$c_data = date('Y-m-d H:i:s');

			mysql_query("UPDATE	`d_letter`
			SET		`job_end_date` = '$c_data'
			WHERE	`id` 	= '$aletter[0]'");
		}
	}
}

function CheckCArtridgeStatus($produsID){
	$status = mysql_fetch_assoc(   mysql_query("SELECT	warehouse_detail.id as `id`
												FROM		warehouse_detail
												WHERE		`warehouse_detail`.`status` = 0 && `warehouse_detail`.`actived`=1 && warehouse_detail.warehouse_id = '$produsID'"));
	return ($status['id'] != '');
}

function check(){
	$data = '';
	$rResult = mysql_query("");
	$aRow = mysql_fetch_array( $rResult );
	$data = $aRow[0];
	return $data;
}

function GetCartridge($produsID){
	$rResult = mysql_query("SELECT		`warehouse`.`end_date` as `end_time`,
										CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `client`,
										`warehouse`.`producing_status` as `status`,
										`warehouse`.`d_letter_id` as `d_letter`,
										`warehouse`.`a_letter_id` as `a_letter`,
										`warehouse`.`id` as `warehouse_id`
							FROM		`warehouse`
							RIGHT JOIN	(`client_objects` RIGHT JOIN `client` ON `client`.`id` = `client_objects`.`client_id`) ON `client_objects`.`id` = `warehouse`.`client_object_id`
							WHERE `warehouse`.`id` = '$produsID'");
	$aRow = mysql_fetch_array( $rResult );
	return 	$aRow;
}

function GetWarehouseId($barcode){
	$rResult = mysql_query("SELECT		warehouse.id
							FROM		warehouse
							LEFT JOIN	warehouse_detail ON warehouse_detail.warehouse_id = warehouse.id
							WHERE		warehouse.producing_status IN (0,1) && warehouse_detail.barcode = '$barcode'
							ORDER BY	warehouse_detail.warehouse_id DESC
							LIMIT 1");
	$aRow = mysql_fetch_array( $rResult );
	if($aRow != ''){
		return 	$aRow[id];
	}else{
		global $error;
		$error = 'შტრიხკოდი არასწორია!';
	}

}

function GetPage($aRow = ''){
		$letter		  = '';
		$letterStatus = '';
		$end_time	  = $aRow['end_time'];
		$client		  = $aRow['client'];
		$status		  = $aRow['status'];
		if( $aRow['d_letter'] != ''){
			$letter			= 'დ - '.$aRow['d_letter'];
			$letterStatus	= 2;
		}else if( $aRow['a_letter'] != ''){
			$letter			= 'ა - '.$aRow['a_letter'];
			$letterStatus	= 1;
		}else{
			$letter			= 'R- '.$aRow['warehouse_id'];
			$letterStatus	= 0;
		}

		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center">
						<th  colspan="2">
							<label for="client_object">ბარათი № <span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$letter.'</span></label>
						</th>
					</tr>
					<tr align="center">
						<th  colspan="2" >
							<label for="client_object">დასრულების თარიღი<span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$end_time.'</span></label>
						</th>
					</tr>
					<tr align="center">
						<th colspan="2">
							<label for="client_object">კლიენტი<span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$client.'</span></label>
						</th>
					</tr>

				</table>
			</fieldset>

			<div class="clear"></div>
			<fieldset>
		    	<legend>პრინტერები</legend>
				 <div class="produse">
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
									<input style="display:inline; " id="barcode_input1" type="text" maxlength="15"/>
					        	</div>
					            <div id="dynamic">
					                <table class="display" id="prod_list" >
					                    <thead>
						                        <tr id="datatable_header">
						                            <th>ID</th>
						                            <th  style=" width : 100%">შტრიხკოდი </th>
													<th  style=" width : 100%">მოდელი</th>
													<th class="check"></th>
						                        </tr>
					                    </thead>
										<thead>
						                        <tr class="search_header">
							                            <th class="colum_hidden">
							                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
							                            </th>
							                            <th>
							                            	<input type="text" name="search_barcode" value="ფილტრი" class="search_init" />
							                            </th>
														<th>
															<input type="text" name="search_model" value="ფილტრი" class="search_init" />
														</th>
														<th>
														</th>
							                     </tr>
					                    </thead>
					                </table>
					            </div>
					        </div>
					    </div>
					</div>
				</div>
			</fieldset>
		<!-- ID -->
	    </div>
		<div class="inner-table" >
			<input type="hidden" style="" name="search_id" class="search_init"  id="producId" value="'.$aRow['warehouse_id'].'"/>
		</div>
		<div class="inner-table" >
			<input type="hidden" style="" name="search_id" class="search_init"  id="produsingStatus" value="'.$status.'"/>
		</div>
		<div class="inner-table" >
			<input type="hidden" style="" name="search_id" class="search_init"  id="letterStatus" value="'.$letterStatus.'"/>
		</div>
	    <div id="add-edit–cartridge-form" class="form-dialog" title="ბლანკი">
	    	<!-- aJax -->
		</div> 		';
	return $data;

}

?>